package com.liuxu.topnews.acitivity;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.liuxu.topnews.AccessService;
import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.R;
import com.liuxu.topnews.Service;
import com.liuxu.topnews.Manager.UpdateManager;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
//
    public boolean check=true;
    public static int version;
    private int index;
    private int currentTabIndex=0;
    private Fragment[] mFragments;
    public static List<CheckBox> checkBoxList;
    public Button button1;
    public Button button2;
    public Button button3;
//    public Button button4;
    public Switch switch1;
    public Spinner spinnerCardNumber;
    public EditText editText1;
    public View view;
    public MainActivity parentActivity;
    public Intent intent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.home_fragment, container, false);
        parentActivity= (MainActivity ) getActivity();
        initView();
        initListener();

        return view;
    }
    private void initView() {
        button1 = (Button)view.findViewById(R.id.button1);
        button2 = (Button)view. findViewById(R.id.button2);
        button3 = (Button)view. findViewById(R.id.button3);
//        button4 = (Button)view. findViewById(R.id.button4);
        switch1=(Switch)view.findViewById(R.id.switch1);
        CheckBox CheckBox1=(CheckBox)view.findViewById(R.id.CheckBox1);
        CheckBox CheckBox2=(CheckBox)view.findViewById(R.id.CheckBox2);
        CheckBox CheckBox3=(CheckBox)view.findViewById(R.id.CheckBox3);
        CheckBox CheckBox4=(CheckBox)view.findViewById(R.id.CheckBox4);
        CheckBox CheckBox5=(CheckBox)view.findViewById(R.id.CheckBox5);
        CheckBox CheckBox6=(CheckBox)view.findViewById(R.id.CheckBox6);
        CheckBox CheckBox7=(CheckBox)view.findViewById(R.id.CheckBox7);
        CheckBox CheckBox8=(CheckBox)view.findViewById(R.id.CheckBox8);
        CheckBox CheckBox9=(CheckBox)view.findViewById(R.id.CheckBox9);
        CheckBox CheckBox10=(CheckBox)view.findViewById(R.id.CheckBox10);
        CheckBox CheckBox11=(CheckBox)view.findViewById(R.id.CheckBox11);
        CheckBox CheckBox12=(CheckBox)view.findViewById(R.id.CheckBox12);
        CheckBox CheckBox13=(CheckBox)view.findViewById(R.id.CheckBox13);
        CheckBox CheckBox14=(CheckBox)view.findViewById(R.id.CheckBox14);
        CheckBox CheckBox15=(CheckBox)view.findViewById(R.id.CheckBox15);
        CheckBox CheckBox16=(CheckBox)view.findViewById(R.id.CheckBox16);
        CheckBox CheckBox17=(CheckBox)view.findViewById(R.id.CheckBox17);
        CheckBox CheckBox18=(CheckBox)view.findViewById(R.id.CheckBox18);
        CheckBox CheckBox19=(CheckBox)view.findViewById(R.id.CheckBox19);
        CheckBox CheckBox20=(CheckBox)view.findViewById(R.id.CheckBox20);
        CheckBox CheckBox21=(CheckBox)view.findViewById(R.id.CheckBox21);
        CheckBox CheckBox22=(CheckBox)view.findViewById(R.id.CheckBox22);
        CheckBox CheckBox23=(CheckBox)view.findViewById(R.id.CheckBox23);
        CheckBox CheckBox24=(CheckBox)view.findViewById(R.id.CheckBox24);
        CheckBox CheckBox25=(CheckBox)view.findViewById(R.id.CheckBox25);
        CheckBox CheckBox26=(CheckBox)view.findViewById(R.id.CheckBox26);
        CheckBox19.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        checkBoxList = new ArrayList<CheckBox>();
        checkBoxList.add(CheckBox1);
        checkBoxList.add(CheckBox2);
        checkBoxList.add(CheckBox3);
        checkBoxList.add(CheckBox4);
        checkBoxList.add(CheckBox5);
        checkBoxList.add(CheckBox6);
        checkBoxList.add(CheckBox7);
        checkBoxList.add(CheckBox8);
        checkBoxList.add(CheckBox9);
        checkBoxList.add(CheckBox10);
        checkBoxList.add(CheckBox11);
        checkBoxList.add(CheckBox12);
        checkBoxList.add(CheckBox13);
        checkBoxList.add(CheckBox14);
        checkBoxList.add(CheckBox15);
        checkBoxList.add(CheckBox16);
        checkBoxList.add(CheckBox17);
        checkBoxList.add(CheckBox18);
        checkBoxList.add(CheckBox19);
        checkBoxList.add(CheckBox20);
        checkBoxList.add(CheckBox21);
        checkBoxList.add(CheckBox22);
        checkBoxList.add(CheckBox23);
        checkBoxList.add(CheckBox24);
        checkBoxList.add(CheckBox25);
        checkBoxList.add(CheckBox26);
        parentActivity.tokenSet(checkBoxList);
        spinnerCardNumber = (Spinner)view.findViewById(R.id.spinner1);
        editText1 =(EditText)view.findViewById(R.id.word);
        parentActivity.tokenSet(editText1);
        TextView visiontext =(TextView)view.findViewById(R.id.TextView11);
        version =MainActivity.getVersionCode(parentActivity.getApplicationContext());
        String versionname=MainActivity.getVersion(parentActivity.getApplicationContext());
        visiontext.setText("版本号："+ versionname);
        mFragments=new Fragment[]{new HomeFragment(),new InfoFragment()};
        intent = new Intent(parentActivity.getApplicationContext(), AccessService.class);
    }
    private void initListener() {

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<checkBoxList.size();i++){
                    checkBoxList.get(i).setChecked(!check);
                }
                check=!check;
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.permission = false;
                Log.e("begin", "onDestroy");
                MyApplication.exit = true;
//                getActivity().stopService(intent);
                System.exit(0);
            }
        });
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.permission=false;
                String action =spinnerCardNumber.getSelectedItem().toString();
                String token=editText1.getText().toString();
                String id=parentActivity.getSerial();
                parentActivity.tokenSave(token);
                parentActivity.tokenSave(checkBoxList);
                Service service=new Service();
                service.checkUpdate(version);
                if (MyApplication.update){
                    UpdateManager updateManager=new UpdateManager();
                    updateManager.mContext=getContext();
                    updateManager.showNoticeDialog();
                }
                if (MyApplication.update&&MyApplication.must){
                    Toast.makeText(parentActivity.getApplicationContext(),"此版本为强制更新，否则无法运行",Toast.LENGTH_LONG).show();
                }else {
                    if(parentActivity.checkAccessibilityEnabled("com.liuxu.topnews/.AccessService")){
                        service.run(version,action,token,id,parentActivity.handler);
                        boolean choice[]= new boolean [checkBoxList.size()];
                        String appnames[]=new String[checkBoxList.size()];
                        for(int i=0;i<checkBoxList.size();i++){
                            choice[i]=checkBoxList.get(i).isChecked();
                            appnames[i]=checkBoxList.get(i).getText().toString();
                        }
                        intent.putExtra("choices",choice);
                        intent.putExtra("names",appnames);
                        if (MyApplication.permission){
                            parentActivity.startService(intent);//启动servicce服务
                        }
                    }else{
                        Intent Settingintent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
                        startActivity(Settingintent);
                        Toast.makeText(parentActivity.getApplicationContext(),"找到超级头条，打开服务",Toast.LENGTH_LONG).show();
                    }
                }

//
            }
        });
        switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                MyApplication.sleep=switch1.isChecked();
                if (switch1.isChecked()){
                    Log.e("凌晨休息功能", "打开");
                    Toast.makeText(parentActivity.getApplicationContext(),"启用休息功能",Toast.LENGTH_SHORT).show();
                }else {
                    Log.e("凌晨休息功能", "关闭");
                    Toast.makeText(parentActivity.getApplicationContext(),"关闭休息功能",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

}
